package com.sangcomz.fishbun.util;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;

public class RadioWithTextButton extends Component implements Component.DrawTask {

    private float PADDING_TEXT = 20;

    private RadioType radioType = new RadioType.None();
    private Paint textPaint = new Paint();
    private Paint strokePaint = new Paint();
    private Paint circlePaint = new Paint();

    private float textWidth;
    private Rect centerRect;

    public RadioWithTextButton(Context context) {
        this(context,null,"");
    }

    public RadioWithTextButton(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public RadioWithTextButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        initInfo();
        addDrawTask(this);
    }

    private void initInfo() {
        textPaint.setAntiAlias(true);
        textPaint.setFakeBoldText(true);
        strokePaint.setAntiAlias(true);
        circlePaint.setAntiAlias(true);

        textWidth = (getWidth() / 3) * 2 - PADDING_TEXT;
        centerRect = _centerRect();
    }

    private Rect _centerRect() {
        Rect r = new Rect(0, 0, getWidth(), getHeight());
        int width = getWidth() / 4;
        Rect result = new Rect(r.getCenterX() - width, r.getCenterY() - width, getWidth() - width, getHeight() - width);
        return result;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        strokePaint.setStrokeWidth(getWidth() / 18);

        isSelected(new SelectedCallback() {
            @Override
            public void block() {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 3, circlePaint);
            }
        });

        if (radioType instanceof RadioType.RadioText) {

            String text = ((RadioType.RadioText) radioType).getText();
            drawTextCentered(canvas, textPaint, text, getWidth() / 2, getHeight() / 2);

        } else if (radioType instanceof RadioType.RadioDrawable) {

            Element element = ((RadioType.RadioDrawable) radioType).getElement();
            element.setBounds(centerRect);
            element.drawToCanvas(canvas);

        } else if (radioType instanceof RadioType.None) {
            strokePaint.setStyle(Paint.Style.STROKE_STYLE);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 3, strokePaint);
        }
    }

    private void drawTextCentered(Canvas canvas, Paint paint, String text, float cx, float cy) {
        paint.setTextSize((int) textWidth);
        Rect textBounds = paint.getTextBounds(text);
        canvas.drawText(paint, text, cx - textBounds.getCenterX(), cy - textBounds.getCenterY());
    }

    public void setTextColor(int color) {
        textPaint.setColor(new Color(color));
    }

    public void setCircleColor(int color) {
        circlePaint.setColor(new Color(color));
    }

    public void setStrokeColor(int color) {
        strokePaint.setColor(new Color(color));
    }

    public void setText(String text) {
        radioType = new RadioType.RadioText(text);
        invalidate();
    }

    public void setDrawable(Element element) {
        radioType = new RadioType.RadioDrawable(element);
        invalidate();
    }

    public void unselect() {
        radioType = new RadioType.None();
        invalidate();
    }

    private void isSelected(SelectedCallback callback) {
        if (!(radioType instanceof RadioType.None) && callback != null) {
            callback.block();
        }
    }

    interface SelectedCallback {
        void block();
    }
}
